package gui.geometry;

import gui.Paintbrush;

import java.awt.BasicStroke;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;

import model.NetworkNode;
import model.NetworkObject;

public class TextDescriptor extends GeometryDescriptor {

	private FontMetrics fm;
	
	public TextDescriptor(NetworkObject object, Point point, int index, FontMetrics fm) {
		super(object, point);
		this.index = index;
		this.fm = fm;
	}

	public static GeometryDescriptor contains(Point point, NetworkNode node, FontMetrics fm) {
		
		int w = fm.stringWidth(node.getName());
		int h = fm.getHeight() - 5;
//		int h = fm.getHeight() - 9;

		if(node.getName().length() == 0) {
			w = 6;
			if(bounded(point, node, w, h)) {
				return new TextDescriptor(node, point, 0, fm);
			}
			else {
				return null;
			}
		}
		
		if(!bounded(point, node, w, h)) {
			return null;
		}
		
		String name = node.getName();
		int distance = (int) (point.x - (node.getX() - w / 2));
		int width = 0;
		int i = -1;
		while(width < distance && i < name.length() - 1) {
			width += fm.stringWidth(name.charAt(++i) + "");
		}
		
		int characterWidth = fm.stringWidth(name.charAt(Math.max(i, 0)) + "");
		if(Math.abs(width - characterWidth - distance) > Math.abs(width - distance)) {
			i++;
		}
				
		return new TextDescriptor(node, point, Math.max(i, 0), fm);
	}
	
	private static boolean bounded(Point point, NetworkNode node, int width, int height) {
		//Bounds Check
		if(point.x < node.getX() - width / 2 - 1 || point.x > node.getX() + width / 2 + 1
				|| point.y < node.getY() - height / 2 - 1 || point.y > node.getY() + height / 2 + 1) {
			return false; 
		}
		return true;
	}
	
	@Override
	public void draw(Graphics2D g) {
		NetworkNode ob = (NetworkNode) object;
		int w = fm.stringWidth(ob.getName());
		int h = fm.getHeight() - 5;
//		int h = fm.getHeight() - 9;
		
		
		String name = ob.getName();
		int width = 0;
		
		for(int i = 0; i < index; i++) {
			width += fm.stringWidth(name.charAt(i) + "");
		}
		
		Point p1 = new Point((int)(ob.getX() - w / 2 + width), (int)(ob.getY() - h / 2));
		Point p2 = new Point((int)(ob.getX() - w / 2 + width), (int)(ob.getY() + h / 2));
		
		g.setStroke(new BasicStroke(2));
		g.setColor(Paintbrush.seaFoam);
		g.drawLine(p1.x, p1.y, p2.x, p2.y);
		g.setStroke(new BasicStroke(1));
	}
	
	public void updateBackspace() {
		String name = object.getName();
		String newName = "";
		for(int i = 0; i < name.length(); i++) {
			if(i != index - 1) {
				newName += name.charAt(i);
			}
			else {
				index--;
			}
		}
		
		((NetworkNode)object).setName(newName);
	}
	
	public void moveCursor(int direction) {
		index += direction;
		if(index < 0) {
			index = 0;
		}
		else if(index > object.getName().length()) {
			index = object.getName().length();
		}
	}
	
	public void typeCharacter(char character) {
		String name = object.getName();
		String newName = "";
		for(int i = 0; i < name.length(); i++) {
			if(i == index) {
				newName += character;
			}
			newName += name.charAt(i);
		}
		if(name.length() == index) {
			newName += character;
		}
		index++;
		
		((NetworkNode)object).setName(newName);
	}
	
	public String toString() {
		return object.getName() + "\tindex: " + index;
	}

}
